﻿using System.Collections;
using System.Collections.Generic;
using BehaviorDesigner.Runtime;
using UnityEngine;


public class EnemyEntity : MovableEntity
{
    private GameObject _target;
    
    protected SpriteRenderer m_spriteRenderer;
    protected Animator _animator;
    
    [Header("行为树方面的参数")]
    public bool isStartingBehavior;

    #region Events
    
    private void Start()
    {
        currentCollider = GetComponent<Collider2D>();
        m_spriteRenderer = GetComponent<SpriteRenderer>();
        
        InitialMovement();

        GetCurrentTile();
    }

    // Update is called once per frame
    #endregion


    #region Behaviors

    public void CalculateTargetTile()
    {
        if (isMoving)
        {
            return;
        }

        Debug.Log("Target :: 准备计算目标Tile");

        GameObject player = GameObject.FindGameObjectWithTag(StaticDataManager.PLAYER_TAG);

        TileHelper.CalculateTileScore(player);

        Vector2 checkBox = new Vector2(move, move);

        Debug.Log("Target :: 寻找最近的移动目标");

        // TODO 寻找这个目标的附近可移动格子
        // TODO 需要优化 最好是移动到掩体后面 除非太远才需要赶路
        this._target = FindMoveToTile(this.gameObject, checkBox);
        
        Debug.Log($"Target :: 最适合的目标位置是 {this._target.transform.position}", _target);
        
        //this.target = target;

        Debug.Log("Target :: 使用A*计算移动到目标最近的路径");
        CalculatePath(); // 使用A*计算到目标位置的路径

        Debug.Log("Target :: 判断目标是否存在");
        if (actualTargetTile)
        {
            Debug.Log("Target :: 目标已发现");
        }

        if (!actualTargetTile)
        {
            Debug.Log("Target :: 目标未发现");
            actualTargetTile = GetTargetTile(gameObject);
        }

        actualTargetTile.isTarget = true;

        TileManager.ShowMoveTiles();
    }

    public bool MoveStep()
    {
        Move();

        if (isMoving)
        {
            if (heading.x < 0 != m_is_facingLeft)
            {
                m_is_facingLeft = heading.x < 0;
                FlipSpriteX();
            }
            
            _animator.Play("Walk");
        }
        else
        {
            _animator.Play("Idle");
        }

        return isMoving;
    }

    #endregion

    public static GameObject FindValuableTile(GameObject target, Vector2 checkBox)
    {
        // 根据当前的检测BOX来选择分数最高的一个格子
        Collider2D[] collides = Physics2D.OverlapBoxAll(target.transform.position, checkBox, 0,
            LayerMask.GetMask(StaticDataManager.TILE_LAYER));

        GameObject targetTile = null;
        int currentScore = int.MinValue;

        foreach (Collider2D collider in collides)
        {
            if (!collider) continue;
            TileMonoBehaviour tile = collider.gameObject.GetComponent<TileMonoBehaviour>();

            if (tile.isOcupied) continue;

            int tileScore = tile.GetScore();

            if (tileScore <= currentScore) continue;

            currentScore = tileScore;
            targetTile = tile.gameObject;
        }

        return targetTile;
    }


    private void CalculatePath()
    {
        TileMonoBehaviour targetTile = GetTargetTile(_target);
        Debug.Log($"Target found {_target.gameObject.transform.position}", _target.gameObject);
        FindPath(targetTile);
    }

    protected virtual GameObject FindMoveToTile(GameObject target, Vector2 checkBox)
    {
        return FindValuableTile(this.gameObject, checkBox);
    }
}